package a51qindian.com.comprehensive.activity;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.li.utils.ToastUtil;
import com.li.widget.DoubleTextView;
import com.li.widget.MultipleLayout;
import com.li.widget.TopNavigationBar;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import a51qindian.com.comprehensive.R;
import a51qindian.com.comprehensive.base.QDBaseActivity;
import a51qindian.com.comprehensive.model.BaseModel;
import a51qindian.com.comprehensive.model.ShopDetailInfo;
import a51qindian.com.comprehensive.net.ObservableHelper;
import a51qindian.com.comprehensive.net.RetrofitCreator;
import a51qindian.com.comprehensive.net.SelectorObserver;
import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;

public class CityShopDetailActivity extends QDBaseActivity {

    @BindView(R.id.top_navigation)
    TopNavigationBar mTopNavigation;
    @BindView(R.id.btn_delete)
    Button mBtnDelete;
    @BindView(R.id.layout_detail)
    LinearLayout mLayoutDetail;
    @BindView(R.id.layout_content)
    MultipleLayout mLayoutContent;
    @BindView(R.id.img_header)
    ImageView mImgHeader;
    @BindView(R.id.txt_shop_name)
    DoubleTextView mTxtShopName;
    @BindView(R.id.txt_phone)
    DoubleTextView mTxtPhone;
    @BindView(R.id.txt_avg_consumption)
    DoubleTextView mTxtAvgConsumption;
    @BindView(R.id.txt_discount)
    DoubleTextView mTxtDiscount;
    @BindView(R.id.txt_device_num)
    DoubleTextView mTxtDeviceNum;
    @BindView(R.id.txt_address)
    DoubleTextView mTxtAddress;
    @BindView(R.id.txt_owner_name)
    DoubleTextView mTxtOwnerName;
    @BindView(R.id.txt_commission)
    DoubleTextView mTxtCommission;
    private String mUid;
    private String mShopId;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_city_shop_detail;
    }

    @Override
    protected void initSth() {
        super.initSth();
        mTopNavigation.setNavigationBarListener(this);

        mUid = getIntent().getStringExtra("uid");
        mShopId = getIntent().getStringExtra("shop_id");
        showShopDetail(mUid, mShopId);


    }

    private void showShopDetail(String uid, String shopId) {
        mLayoutContent.onLoading();
        Map<String, String> map = new HashMap<>();
        map.put("uid", uid);
        map.put("shop_id", shopId);
        ObservableHelper.createObservable(RetrofitCreator.createService().shopDetail(map))
                .subscribe(new SelectorObserver<BaseModel<ShopDetailInfo>>() {
                    @Override
                    protected void onErrorCodeInfo(int code, String info) {
                        mLayoutContent.onFailure();
                        ToastUtil.showToast(CityShopDetailActivity.this, info);
                    }

                    @Override
                    protected void onSuccess(BaseModel<ShopDetailInfo> shopDetailInfoBaseModel) {
                        mLayoutContent.onSuccess();
                        ShopDetailInfo detailInfo = shopDetailInfoBaseModel.getData();
                        Glide.with(CityShopDetailActivity.this).load(detailInfo.getTitlepic()).into(mImgHeader);
                        mTxtShopName.setTxtRight(detailInfo.getName());
                        mTxtPhone.setTxtRight(detailInfo.getMobile());
                        mTxtAddress.setTxtRight(detailInfo.getAddress());
                        mTxtAvgConsumption.setTxtRight(detailInfo.getAve_price());

//                        mTxtDiscount.setTxtRight(detailInfo.getDiscount_info().toString());

                        mTxtDeviceNum.setTxtRight(detailInfo.getNumber());

                        String commission = detailInfo.getCommission() + "%";
                        mTxtCommission.setTxtRight(commission);
                        mTxtOwnerName.setTxtRight(detailInfo.getShopowner());
                    }

                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mLayoutContent.onFailure();
                    }
                });
    }


    private void deleteShop(String uid, String shopId) {
        mLayoutContent.onLoadingWithContent("正在删除商铺");
        Map<String, String> map = new HashMap<>();
        map.put("uid", uid);
        map.put("shop_id", shopId);
        ObservableHelper.createObservable(RetrofitCreator.createService().deleteShop(map))
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(@NonNull String s) {
                        mLayoutContent.onSuccess();
                        try {
                            JSONObject object = new JSONObject(s);
                            int code = object.getInt("code");
                            if (code != 1) {
                                ToastUtil.showToast(CityShopDetailActivity.this, object.getString("info"));
                            } else {
                                ToastUtil.showToast(CityShopDetailActivity.this, "删除成功");
                                finish();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mLayoutContent.onSuccess();
                        ToastUtil.showToast(CityShopDetailActivity.this, "删除失败");
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }


    @OnClick({R.id.btn_delete, R.id.txt_commission})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_delete:
                AlertDialog.Builder builder = new AlertDialog.Builder(CityShopDetailActivity.this)
                        .setTitle("提示")
                        .setMessage("是否确认删除该商铺")
                        .setNegativeButton("取消", null)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                deleteShop(mUid, mShopId);
                            }
                        });
                builder.create().show();
                break;
            case R.id.txt_commission:
                View commissionView = getLayoutInflater().inflate(R.layout.layout_commission, null);
                final EditText editText = commissionView.findViewById(R.id.et_commission);
                AlertDialog.Builder builderCommission = new AlertDialog.Builder(CityShopDetailActivity.this)
                        .setTitle("输入分成比例")
                        .setView(commissionView)
                        .setNegativeButton("取消", null)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                setShopCommission(mUid, mShopId, editText.getText().toString());
                            }
                        });
                builderCommission.create().show();
                break;
        }
    }

    /**
     * 设置分成比例
     * @param uid   用户编号
     * @param shopId    商户编号
     * @param commission    分成比例1-100
     */
    private void setShopCommission(String uid, String shopId, final String commission) {
        mLayoutContent.onLoadingWithContent("设置分成中");
        Map<String, String> map = new HashMap<>();
        map.put("uid", uid);
        map.put("shop_id", shopId);
        map.put("money", commission);
        ObservableHelper.createObservable(RetrofitCreator.createService().shopCommission(map))
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(@NonNull String s) {
                        mLayoutContent.onSuccess();
                        try {
                            JSONObject o=new JSONObject(s);
                            int code=o.getInt("code");
                            if (code!=1) {
                                ToastUtil.showToast(CityShopDetailActivity.this,o.getString("info"));
                            }else {
                                ToastUtil.showToast(CityShopDetailActivity.this,o.getString("data"));
                                mTxtCommission.setTxtRight(commission+"%");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mLayoutContent.onSuccess();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
